<template>
  <div class="goodList">
    <van-pull-refresh v-model="refreshing"
                      @refresh="onRefresh"
                      success-text="加载完成"
                      success-duration="500">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsList">
        <ul class="goods-ul">
          <div class="goods-item"
               v-for="item in goodsList" :key="item.id"
               @click="goDetail('/home/goodsinfo',item.id)">
            <img v-lazy="item.img_url" alt="">
            <div class="info">
              <h2>{{item.title}}</h2>
              <div class="price">
                <span class="nowPrice">现价￥{{item.sell_price}}</span>
                <span class="oldPrice">原价￥{{item.market_price}}</span>
              </div>
              <div class="status">
                <span class="hot">热卖中</span>
                <span class="surplus">剩余{{item.stock_quantity}}</span>
              </div>
            </div>
          </div>
        </ul>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
export default {
  name: 'index',
  data: () => ({
    goodsList: [],
    pageIndex: 0,
    loading: false,
    finished: false,
    refreshing: false
  }),
  methods: {
    async getGoodsList () {
      this.pageIndex++
      const { message } = await this.$http.getGoodsList(this.pageIndex)
      if (message.length <= 0) {
        this.finished = true
        return
      }
      this.goodsList.push(...message)
      this.loading = false
    },
    async onRefresh () { // 刷新列表
      // 初始化参数
      this.finished = false
      this.loading = true
      await this.getGoodsList()
      this.refreshing = false
    }
  }
}
</script>

<style lang="scss" scoped>
  .goodList {
    padding-bottom: 60px;

    .goods-ul {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
      padding: 30px 20px 0;
      box-sizing: border-box;
      justify-content: space-between;

      .goods-item {
        display: flex;
        padding: 10px;
        border: 1px solid #eeeeee;
        width: 45%;
        flex-direction: column;
        margin-bottom: 15px;

        .info {
          padding: 0 10px;

          h2 {
            margin: 10px 0;
            line-height: 30px;
          }

          .price {
            .nowPrice {
              color: red;
            }

            .oldPrice {
              font-size: 14px;
              text-decoration: line-through;
              margin-left: 20px;
              color: #ccc;
            }
          }

          .status {
            display: flex;
            justify-content: space-between;
            margin-top: 6px;
            font-size: 14px;

            .hot {
              color: orange;
            }

            .surplus {
              color: #666666;
            }
          }
        }
      }
    }
  }
</style>
